const db = require("./connectDB")

module.exports = function(app, db) {
    /* 根据number获取明信片内容 */
    app.get("/postcard", async(req, res) => {
        const number = req.query.number
        const result = await runSql(`select * from postcard where number='${number}'`)
        if(result.length === 0) {
            response({
                res,
                status: 500,
                message: "未查询到你的凭证"
            })
        } else {
            response({
                res,
                data: result[0]
            })
        }
    })
}

function runSql(sql, data) {
    return new Promise((resolve, reject) => {
        db.query(sql, data, (err, res) => {
            if(err) {
                reject(err)
                throw err
            }
            resolve(res)
        })
    })
}
function response({res,status=200,message='', data={}}) {
    res.status(status).send({
        success: status === 200,
        message,
        data
    })
}